package com.xiguthings.waka.web.controller.wechat.wx;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import com.alibaba.fastjson.JSONObject;



public class Xml2Json {
	public static JSONObject readFromStream(InputStream is) {
		SAXBuilder builder = new SAXBuilder();
		Document doc = null;
		try {
			doc = builder.build(is);
			Element root = doc.getRootElement();
			JSONObject json = new JSONObject();
			List<Element> list = root.getChildren();
			for (Element element : list) {
				json.put(element.getName(), element.getTextTrim());
			};
			return json;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static JSONObject read(String xml) {
		JSONObject rs = null;
		try {
			InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			rs = readFromStream(is);
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}
}
